<template>
	<div id="writeDaily" class="w-h-100" style="box-sizing: border-box; overflow: hidden">
		<div class="w-h-100 ver-flex">
			<el-card class="box-card" shadow="hover" :body-style="{ paddingBottom: '0' }">
				<search v-bind="queryDailySearchConfig">
					<template #footer>
						<el-button type="primary" icon="ele-Search" @click="query" :style="{ display: $hasPer('queryDaily:query') ? '' : 'none' }"> {{ $t('6byUl8U10pYbuLt9gcer') }} </el-button>
						<!-- <el-button icon="ele-Plus" type="primary" @click="addAction"  :style="{ display: $hasPer('queryDaily:add') ? '' : 'none' }"> 新增 </el-button> -->
						<el-button icon="ele-Document" type="primary" @click="detailAction" :style="{ display: $hasPer('queryDaily:detail') ? '' : 'none' }"> {{ $t('gSq2m2JIcjCxQFvDceJj8') }} </el-button>
						<!-- <el-button icon="ele-Edit" type="primary" @click="changeAction"  :style="{ display: $hasPer('queryDaily:change') ? '' : 'none' }"> 修改 </el-button>
						<el-button icon="ele-Delete" type="danger" @click="deleteAction"  :style="{ display: $hasPer('queryDaily:remove') ? '' : 'none' }"> 删除 </el-button> -->
					</template>
				</search>
			</el-card>
			<el-card class="w-h-100 flex-1" shadow="hover" :body-style="{ paddingBottom: '0' }" style="margin-top: 8px">
				<inscroll-table
					:data-list="list"
					v-bind="queryDailyTableConfig"
					:dataCount="Number(totalRows)"
					:handle-size-change="handleSizeChange"
					:handle-current-change="handleCurrentChange"
					ref="tableRef"
					@single-select="getSelectData"
				>
					<template #date="scope">
						<div>{{ scope.row.date.split(' ')[0] }}</div>
					</template>
					<template #status="scope">
						<!-- vue对于js对象只能解析两层 -->
						<div v-if="scope.row && scope.row.sysOperation">
							{{
								scope.row.sysOperation.state == 1
									? $t('a')
									: scope.row.sysOperation.state == 2
									? $t('b')
									: scope.row.sysOperation.state == 3
									? $t('c')
									: scope.row.sysOperation.state == 4
									? $t('d')
									: $t('e')
							}}
						</div>
					</template>
				</inscroll-table>
			</el-card>
		</div>
		<write-form ref="formRef" @submit="(param: any) => submitHandle(param)"></write-form>
		<daily-detail-form ref="detailFormRef"></daily-detail-form>
	</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import getStaticTableData from '/@/utils/staticGetTableData';
//  获取系统应用列表 ,删除app接口
import { getDailyList, postDailyAction, postDeleteDailyAction, postChangeDailyAction } from '/@/api/platformWork/index';
import { inscrollTable, queryDailyTableConfig } from './config/table/index';
import { search, queryDailySearchConfig } from './config/search/index';
import { writeForm, dailyDetailForm } from './compoment/index';
import { IPostDailyData } from 'src/types/ys/index';
import { ElMessage, ElMessageBox } from 'element-plus';
import useCurrentInstance from '/@/utils/hook/useCurrentInstance';
const { proxy } = useCurrentInstance();
const $t = proxy.$t;
const formRef = ref();
const detailFormRef = ref();
const tableRef = ref();
const { list, totalRows, handleSizeChange, handleCurrentChange, getData } = getStaticTableData(getDailyList, {}, queryDailySearchConfig.searchObj);
const deleteAction = () => {
	if (selectData && selectData.id) {
		ElMessageBox.confirm($t('kfOQpG5xMn9udbeboC6iP'), $t('piOjp1dVaOlnHzZweMZpy'), {
			confirmButtonText: $t('aS2TeoRlOl0vyDe85WYcx'),
			cancelButtonText: $t('3N3XRcGghvk4bP9pEmeiz'),
			type: 'warning',
			center: true,
		})
			.then(async () => {
				const res: any = await postDeleteDailyAction({ Id: selectData.id });
				tableRef.value.radio1 = '999999';
				selectData = {};
				getData();
			})
			.catch(() => {});
	} else {
		ElMessage.warning($t('ocTrMrgEtsRbM5fbVktv9'));
	}
};
let isChange = false; //true 修改 false新增
//新增操作
const addAction = (data: any) => {
	isChange = false;
	formRef.value.addNew(data);
};

//修改操作
const changeAction = () => {
	if (selectData && selectData.id) {
		if (selectData.sysOperation.state == 1 || selectData.sysOperation.state == 2) {
			ElMessage({
				message: $t('sefwNh6T0y62aIb0nLZvU'),
				type: 'warning',
			});
			return;
		}
		isChange = true;
		formRef.value.changeFormAction(selectData);
	} else {
		showTint();
	}
};

//查看详情操作
const detailAction = () => {
	if (selectData && selectData.id) {
		detailFormRef.value.addNew(selectData);
	} else {
		showTint();
	}
};

let selectData: any = {};
//获取用户表格中选中的按钮
const getSelectData = (data: IPostDailyData) => {
	selectData = data;
};
const showTint = () => {
	ElMessage({
		message: $t('6dDOiL6OfZ1MpGdPeF0Pf'),
		type: 'warning',
	});
};
//重置选中按钮状态
const reset = () => {
	tableRef.value.radio1 = '999999';
	selectData = {};
};

//提交表单
const submitHandle = async (param: any) => {
	if (isChange) {
		//修改
		const postData: IPostDailyData = {
			id: selectData.id,
			projectNo: param.projectNo,
			projectName: param.projectName,
			projectSite: param.projectSite,
			date: param.date,
			operationID: param.operationID, //审核流程ID
			progressPercentage: param.proProcess, //项目进度
			description: param.description, //备注
			materialComing: param.materialComing, //来料状况
			materialComing_QU: param.materialComing_QU, //来料状况是否异常
			quality: param.quality,
			quality_QU: param.quality_QU, //质量状况是否异常
			progressStatus: param.progressStatus, //进度状况
			progressStatus_QU: param.progressStatus_QU, //进度状况是否异常
			security: param.security, //安全状况
			security_QU: param.security_QU, //安全状况是否正常
			customerComplaints: param.customerComplaints, //客户反馈情况
			customerComplaints_QU: param.customerComplaints_QU, //客户反馈情况是否异常
			theset: param.theset, //现场人员每天工作安排
			theset_QU: param.theset_QU, //现场人员每天工作安排异常
			workHours: param.workHours, //员工日报工时
		};
		const res: any = await postChangeDailyAction(postData);
		reset();
		formRef.value.formRef.isShowDialog = false;
		getData();
	} else {
		//新增
		const postData1: IPostDailyData = {
			projectNo: param.projectNo,
			projectName: param.projectName,
			projectSite: param.projectSite,
			date: param.date,
			operationID: param.operationID, //审核流程ID
			progressPercentage: param.proProcess, //项目进度
			description: param.description, //审核备注
			materialComing: param.materialComing, //来料状况
			materialComing_QU: param.materialComing_QU, //来料状况是否异常
			quality: param.quality,
			quality_QU: param.quality_QU, //质量状况是否异常
			progressStatus: param.progressStatus, //进度状况
			progressStatus_QU: param.progressStatus_QU, //进度状况是否异常
			security: param.security, //安全状况
			security_QU: param.security_QU, //安全状况是否正常
			customerComplaints: param.customerComplaints, //客户反馈情况
			customerComplaints_QU: param.customerComplaints_QU, //客户反馈情况是否异常
			theset: param.theset, //现场人员每天工作安排
			theset_QU: param.theset_QU, //现场人员每天工作安排异常
			workHours: param.workHours, //员工日报工时
			_xmField: [
				{
					proStartRealTime: param.proStartRealTime,
					proEndRealTime: param.proEndRealTime,
					ProHalfRealTime: param.ProHalfRealTime,
				},
			],
		};
		const res: any = await postDailyAction(postData1);
		formRef.value.formRef.isShowDialog = false;
		getData({
			pageNo: 1,
			pageSize: 10,
		});
	}
};

//查询操作
const query = () => {
	reset();
	if (queryDailySearchConfig.searchObj.StartTime) {
		queryDailySearchConfig.searchObj.StartTime = queryDailySearchConfig.searchObj.StartTime + ' 00:00:00';
		queryDailySearchConfig.searchObj.EndTime = (queryDailySearchConfig.searchObj.StartTime + ' 23:59:59').replace(' 00:00:00', '');
	}
	getData();
};
</script>
<style lang="scss" scoped>
//vue基本样式
#writeDaily {
	:deep(.el-card__body) {
		height: 100%;
	}
}
</style>
